package com.genius.adminmanager.menu.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;
import com.genius.workflow.util.Pattern;
import com.genius.workflow.util.TimeDiff;

@Controller
public class MenuEditorController {
	/**
	 * 跳转到菜单编辑页面
	 */
	@GetMapping("/adminmanager/menu/toEdit")
	public String toEditMenu(){
		return "/views/adminmanager/menu/edit";
	}
	@PostMapping("/adminmanager/menu/edit")
	@ResponseBody
	public JSONObject editMenu(@RequestBody JSONObject json, HttpSession session){
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		JSONObject userJson = (JSONObject)session.getAttribute(SessionJson.currentUser);
		String labelName = userJson.getString(UserJson.name);//获取当前创建者
		if(labelName==null){
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.status, ResponseJson.msg_Session_Failure);
			return response;
		}
		String createTime = TimeDiff.getNowTime(Pattern.yyyyMMddHHmmss);
		String status = json.containsKey(MenuJson.status)?"1":"0";
		String menuId = json.getString(MenuJson.menuId);////当前的菜单id
		String pId = json.getString(MenuJson.parentId);//当前菜单的父id
		String name = json.getString(MenuJson.name);//当前菜单的名称
		String toChangePid = json.getString("toChangePid");//要更新的新的菜单的Pid
		String toChangeMenuId = json.getString("toChangeMenuId");//要更新的新的菜单的menuId

		json.put(MenuJson.status, status);
		json.put(MenuJson.createTime, createTime);
		json.put(MenuJson.createAuthor, labelName);
		String whereField = MenuJson.menuId+","+MenuJson.parentId;
		String table = "t_menu";

		try {
			if(!StringUtils.equals(toChangePid, "-1")&&StringUtils.isNotBlank(toChangePid)&&!StringUtils.equals(pId, toChangeMenuId)){
				//说明要修改的菜单与原先的父菜单不一致了
				String selectField = MenuJson.state+","+MenuJson.type;
				String selectSql = DBTools.getSelectAndWhereSql(selectField, table, whereField);
				String [] selectParms = new String[]{toChangeMenuId,toChangePid};
				JSONObject changeJson = DBTools2.select(selectSql, selectParms);
				//前提是toChangeMenuId不能为空，如果为空，说明没用转移菜单，只是修改其他而已
				if(changeJson == null && !StringUtils.isNotBlank(toChangeMenuId) && !StringUtils.equals(toChangeMenuId, "-1") && !StringUtils.equals(toChangePid, "-1")){
					response.put(ResponseJson.status, ResponseJson.status_Failed);
					response.put(ResponseJson.msg, "目标菜单不存在");
					return response;
				}
				if(changeJson != null && StringUtils.equals("menu", changeJson.getString(MenuJson.type))&&changeJson.getInteger(MenuJson.state)<2){
					response.put(ResponseJson.status, ResponseJson.status_Failed);
					response.put(ResponseJson.msg, "菜单转移新目录下,目标目录级别至少在三级及以上");
					return response;
				}
				MenuUtils.editorMenu(json, table, whereField, menuId, name, pId, toChangeMenuId, toChangePid);
			}else{
				MenuUtils.editorMenu(json, table, whereField, menuId, name, pId, null, null);
			}
			response.put(ResponseJson.msg, "编辑菜单成功");
			response.put(ResponseJson.status, ResponseJson.status_Success);
		} catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, "更新菜单出错");
		}
		return response;
	}
}
